package com.bawei.week.splash

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.mvvm.BaseViewActivity
import com.bawei.week.R
import com.bawei.week.databinding.ActivitySplashBinding
import com.blankj.utilcode.util.SPUtils
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import java.util.Timer
import java.util.TimerTask

@Route(path = "/app/SplashActivity")
@AndroidEntryPoint
class SplashActivity : BaseViewActivity<ActivitySplashBinding>() {
    lateinit var timer : Timer
    var time = 6
    override fun initView() {

        if (SPUtils.getInstance().getBoolean("isFirst",false)){
            ARouter.getInstance().build("/app/LoginActivity").navigation()
            finish()
        }

        timer = Timer()
        lifecycleScope.launch {
            timer.schedule(object : TimerTask(){
                override fun run() {
                    time--
                    lifecycleScope.launch() {
                        binding.tvCountdown.text = "${time}S"
                    }
                    if (time == 0){
                        timer.cancel()
                        ARouter.getInstance().build("/app/LoginActivity").navigation()
                        finish()
                    }
                }
            },0,1000)
        }

        binding.tvSkip.setOnClickListener {
            ARouter.getInstance().build("/app/LoginActivity").navigation()
            SPUtils.getInstance().put("isFirst",true)
            timer.cancel()
            finish()
        }
    }

    override fun initData() {

    }

    override fun onDestroy() {
        super.onDestroy()
        timer.cancel()
    }
}